package com.xjh.serverinspect.util;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * <p>
 *
 * </p>
 *
 * @author xujinghui
 * @since 2024-12-23
 */
public class XmlUtils {

    /**
     * 解析XML文件
     *
     * @param filePath XML文件路径
     * @return
     * @throws ParserConfigurationException
     * @throws IOException
     * @throws SAXException
     */
    public static Document parseXmlFile(String filePath) throws ParserConfigurationException, IOException, SAXException {
        // 解析XML文件
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        // 确保工厂设置为保持注释
        dbFactory.setIgnoringComments(false); // 保留注释
        dbFactory.setIgnoringElementContentWhitespace(false); // 保留空白符
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        return dBuilder.parse(new File(filePath));
    }

    /**
     * 解析XML文件
     *
     * @param inputStream XML文件输入流
     * @return
     * @throws ParserConfigurationException
     * @throws IOException
     * @throws SAXException
     */
    public static Document parseXmlFile(InputStream inputStream) throws ParserConfigurationException, IOException, SAXException {
        // 解析XML文件
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        // 确保工厂设置为保持注释
        dbFactory.setIgnoringComments(false); // 保留注释
        dbFactory.setIgnoringElementContentWhitespace(false); // 保留空白符
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        return dBuilder.parse(inputStream);
    }

    /**
     * 保存XML文件
     *
     * @param document   XML文档
     * @param outputFile XML文件
     * @return
     * @throws TransformerException
     */
    public static boolean saveXmlFile(Document document, File outputFile) throws TransformerException {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();

        // 设置输出属性以保持格式
        transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // 是否缩进
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); // 缩进量
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        if (document.getDoctype() != null) {
            transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, document.getDoctype().getPublicId());
        }

        if (document.getDoctype() != null) {
            transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, document.getDoctype().getSystemId());
        }

        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(outputFile);
        transformer.transform(source, result);
        return true;
    }

}
